package org.laizili.solution.leetcode;

import org.laizili.structure.tree.bitree.TreeNode;

/**
 * <a href="https://leetcode.cn/problems/successor-lcci/">面试题 04.06. 后继者</a>
 * <p>
 * tags: 树的遍历; 二叉搜索树性质
 */
public class Problem0406 {

    private static class Solution {
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            TreeNode successor = null;
            TreeNode cur = root;
            while (cur != null) {
                if (cur.val > p.val) {
                    successor = cur;
                    cur = cur.left;
                } else {
                    cur = cur.right;
                }
            }
            return successor;
        }
    }
}
